<?php

namespace System\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use System\Model\Products;
use Zend\Session\Container;

class ProductsTable extends AbstractTableGateway {

    protected $table = "tblproduct";
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Products());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function loadvideosmodel($row_per, $start, $key) {
        $batdau = 0;
        $batdau = (($start - 1) * $row_per);
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->where->like("videoName", '%' . $key . '%');
        $sqlEx->order('videoId DESC');
        $sqlEx->limit($row_per);
        $sqlEx->offset($batdau);

        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = array();
        foreach ($resultSet as $result) {
            $data[] = $result;
        }
        return $data;
    }

    public function countloadvideos($key) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);

        $sqlEx->where->like("videoName", '%' . $key . '%');
        $sqlEx->order('videoId DESC');
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = array();
        foreach ($resultSet as $result) {
            $data[] = $result;
        }
        return count($data);
    }

    public function checkurlproduct($url) {
        $sqlex = $this->sql->select();
        $sqlex->from($this->table);
        $sqlex->where(array("productLink" => $url));
        $pst = $this->sql->prepareStatementForSqlObject($sqlex);
        $rs = $pst->execute();
        $check = $rs->current();
        if ($check != null) {
            return $url . "-1";
        } else {
            return $url;
        }
    }

   

    public function addproduct(Products $objproduct) {
        $data = $objproduct->getData();
        $sqlEx = $this->sql->insert();
        $sqlEx->into($this->table);
        $sqlEx->values($data);
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        $abc = $rs->getGeneratedValue();
        return $abc;
    }

    public function status($videosId, $active) {
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set(array("videoActive" => $active));
        $sqlEx->where(array("videoId" => $videosId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $pst->execute();
    }

    public function delImg($videosId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("videoImg" => "videoImg", "videoImgThumb" => "videoImgThumb"));
        $sqlEx->where(array("videoId" => $videosId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        $data = $rs->current();
        $urlImg = $data["videoImg"];
        $urlImgThumb = $data["videoImgThumb"];
        $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/");
        @unlink($dirpath . $urlImg);
        @unlink($dirpath . $urlImgThumb);
    }

    public function delvideos($videosId) {
        $sqlEx = $this->sql->delete();
        $sqlEx->from($this->table);
        $sqlEx->where(array("videoId" => $videosId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $check = $pst->execute();
        if ($check != null) {
            return true;
        } else {
            return false;
        }
    }

    public function getdataedit($videosId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array(
            "videoId" => "videoId",
            "videoName" => "videoName",
            "videoLink" => "videoLink",
            "videoDescription" => "videoDescription",
            "videoImgThumb" => "videoImgThumb",
            "videoContent" => "videoContent",
        ));
        $sqlEx->where(array("videoId" => $videosId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = $resultSet->current();
        return $data;
    }

    public function updatevideos($data, $videosId) {
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set($data);
        $sqlEx->where(array("videoId" => $videosId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

}
